﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Tos2Google
{
    public delegate void AutoAdvancerGoEventHandler();
    public partial class AutoAdvancer : UserControl
    {
        public AutoAdvancer()
        {
            InitializeComponent();
            if (!IsInDesigner)
                timer1.Enabled = true;
        }

        public event AutoAdvancerGoEventHandler Go;

        protected static bool IsInDesigner
        {
            get { return (Assembly.GetEntryAssembly() == null); } // http://bytes.com/topic/c-sharp/answers/448482-detecting-if-userconrtol-designer-run-time
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value -= 2;
            if (progressBar1.Value == 0)
            {
                Disable();
                if (Go != null)
                    Go(); 
            }
        }

        private bool disabled = false;
        public void Disable()
        {
            if (disabled)
                return;
            disabled = true;
            timer1.Enabled = false;
            this.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Disable();
        }

        internal void BehaveAsAutoLoginWants(AutoLogin.PreferredLoginMethodEnum preferredLoginMethod)
        {
            if (preferredLoginMethod == AutoLogin.PreferredLoginMethodEnum.AllManual)
            {
                Disable();
                return;
            }
            if (preferredLoginMethod == AutoLogin.PreferredLoginMethodEnum.AutoLogin)
                progressBar1.Value = 2; // just 2 from 100, basically instantly
        }
    }
}
